package com.uplift.mgt.info.service.impl;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

import org.apache.commons.fileupload.FileItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.github.pagehelper.PageHelper;
import com.uplift.common.Const;
import com.uplift.common.utils.UUIDGenerater;
import com.uplift.mgt.info.dao.IInfoMapper;
import com.uplift.mgt.info.model.Info;
import com.uplift.mgt.info.service.IInfoService;
import com.uplift.sec.SecurityContext;

@Service
@Transactional
public class InfoServiceImpl implements IInfoService {

	@Autowired
	private IInfoMapper infoMapper;

	@Override
	public List<Map<String, Object>> queryInfo(Info info) {
		PageHelper.startPage(info.getPage(), info.getLimit());
		return this.infoMapper.selectInfo(info);
	}

	@Override
	public int saveInfo(Info info) {
		try {
			CommonsMultipartFile cmf = info.getFile();
			FileItem item = cmf.getFileItem();
			String fileName = item.getName();
			String fileExt = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
			String newFileName = UUID.randomUUID().toString().replaceAll("-", "") + fileExt;
			String rfilePath = "info/";
			File newFileDir = new File(Const.DIR_UPLOAD_BASE, rfilePath);
			if (!newFileDir.exists()) {
				newFileDir.mkdirs();
			}
			File file = new File(newFileDir, newFileName);
			item.write(file);
			// 设置图片路径
			info.setCoverPic(rfilePath + newFileName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Map<String, Object> userMap = SecurityContext.getSessionUser();
		info.setId(UUIDGenerater.generate());
		info.setCreateUser(Objects.toString(userMap.get("id")));
		info.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		return this.infoMapper.insertInfo(info);
	}

	@Override
	public int removeInfo(Info info) {
		return this.infoMapper.deleteInfo(info);
	}

	@Override
	public int modifyInfo(Info info) {
		CommonsMultipartFile cmf = info.getFile();
		// 如果文件不等于空，则有新图片需要修改
		if (cmf != null) {
			try {
				FileItem item = cmf.getFileItem();
				String fileName = item.getName();
				String fileExt = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
				String newFileName = UUID.randomUUID().toString().replaceAll("-", "") + fileExt;
				String rfilePath = "info/";
				File newFileDir = new File(Const.DIR_UPLOAD_BASE, rfilePath);
				if (!newFileDir.exists()) {
					newFileDir.mkdirs();
				}
				File file = new File(newFileDir, newFileName);
				item.write(file);
				// 设置图片路径
				info.setCoverPic(rfilePath + newFileName);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return this.infoMapper.updateInfo(info);
	}

	@Override
	public Map<String, Object> queryInfoDetail(Info info) {
		List<Map<String, Object>> list = this.queryInfo(info);
		if (list != null && list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

}
